package felix.fuse.mount.client;

import com.amazonaws.util.StringUtils;
import felix.fuse.mount.service.ContextContainer;
import felix.fuse.mount.service.S3Fuse;
import felix.fuse.mount.util.DesUtil;
import org.apache.commons.cli.CommandLine;
import org.apache.commons.cli.CommandLineParser;
import org.apache.commons.cli.DefaultParser;
import org.apache.commons.cli.Options;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.nio.file.FileSystems;

public class FuseS3Main {
    private static final Logger log = LoggerFactory.getLogger(FuseS3Main.class);

    private static final Options options = new Options();

    public static void main(String[] args) {
        options.addOption("u", "username", true, "s3 username,example: -u test");
        options.addOption("p", "password", true, "s3 password,example: -p 123456");
        options.addOption("h", "host", true, "s3 host,example: -h http://ip:port");
        options.addOption("b", "bucketName", true, "s3 bucket,example: -b bucket");
        options.addOption("m", "mount", true, "mount endpoint,example: -m /tmp/mount");
        options.addOption("readonly", "readonly", true, "mount readonly,example: -readonly false");
        CommandLineParser parser = new DefaultParser();
        ContextContainer.init();
        S3Fuse s3Fuse = ContextContainer.get(S3Fuse.class);
        try {
            CommandLine cli = parser.parse(options, args);
            String username = getOrCheckOptionValue("u", cli);
            String password = getOrCheckOptionValue("p", cli);
            String endpoint = getOrCheckOptionValue("h", cli);
            String mount = getOrCheckOptionValue("m", cli);
            String bucketName = getOrCheckOptionValue("b", cli);
            String readOnly = cli.getOptionValue("readonly");
            if (Boolean.parseBoolean(readOnly)) {
                s3Fuse.setReadOnly(true);
            }
            s3Fuse.setRootBucket(bucketName);
            s3Fuse.connection(username, password, endpoint);
            s3Fuse.mount(FileSystems.getDefault().getPath(mount), false, false);
        } catch (IllegalArgumentException argumentException) {
            log.error("argument is not available," + argumentException.getMessage());
            System.exit(0);
        } catch (Exception e) {
            log.error(e.getMessage());
            System.exit(0);
        }
        while (true) {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                s3Fuse.umount();
                System.exit(0);
            }
        }
    }

    public static String getOrCheckOptionValue(String opt, CommandLine cli) throws IllegalArgumentException {
        String optionValue = cli.getOptionValue(opt);
        if (StringUtils.isNullOrEmpty(optionValue)) {
            throw new IllegalArgumentException(options.getOption(opt).getDescription());
        }
        // 值以DES@开头的均为DES加密,需要解密
        if (optionValue.startsWith("DES@")) {
            return DesUtil.decrypt(optionValue.substring(4), DesUtil.KEY);
        }
        return optionValue;
    }
}
